package com.xiangwei.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;











import com.xiangwei.dao.ExtraColumnMapper;
import com.xiangwei.dao.ImageMapper;
import com.xiangwei.dao.ProductMapper;
import com.xiangwei.dao.VendorMapper;
import com.xiangwei.dto.DropDownDto;
import com.xiangwei.entity.ExtraColumn;
import com.xiangwei.entity.Image;
import com.xiangwei.entity.Vendor;
import com.xiangwei.service.IVendorService;
import com.xiangwei.util.Common;
import com.xiangwei.view.VendorView;

@Service
public class VendorServiceImpl implements IVendorService {
	private static final Logger logger = LoggerFactory
			.getLogger(VendorServiceImpl.class);
	
    @Resource
    private VendorMapper vendorMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ImageMapper imageMapper;
    @Resource
    private ExtraColumnMapper extraColumnMapper;

	@Override
	public List<Vendor> findVendorList(Map<Object,Object> map) {
		List<Vendor> vendorList = new ArrayList<Vendor>();
		vendorList = vendorMapper.findVendorList(map);
		return vendorList;
	}
	
	@Override
	public Vendor selectByPrimaryKey(Integer vendorId) {
		Vendor vendor = new Vendor();
		vendor = vendorMapper.selectByPrimaryKey(vendorId);
		return vendor;
	}
	
	@Override
	@Transactional
	public void insertVendor(Vendor vendor,String[] cName,String[] cValue){
		if(Common.isNullOrEmpty(vendor)){
			logger.error("传入参数为空");
			return;
		}
		Map<Object,Object> map = new HashMap<Object, Object>();
		map.put("vendorName", vendor.getVName()==null? null : vendor.getVName().trim());
		int count = vendorMapper.findVendorNameCount(map);
		if(count > 0){
			logger.error("公司名称重复");
			return;
		}
		vendorMapper.insertSelective(vendor);
		if(Common.isNullOrEmpty(cName) || Common.isNullOrEmpty(cValue)  ||
				cName.length == 0 || cValue.length == 0 ){
			return;
		}
		Vendor v = vendorMapper.selectVendorByName(vendor.getVName());
		for(int i=0;i<cName.length;i++){
			if(!Common.isEmptyString(cName[i]) || !Common.isEmptyString(cValue[i])){
				continue;
			}
			ExtraColumn e = new  ExtraColumn();
			e.setPid(v.getId());
			e.settType(Short.parseShort("0"));//0表示厂商，1表示产品
			e.setcName(cName[i]);
			e.setcValue(cValue[i]);
			extraColumnMapper.insertSelective(e);
		}
	}

	@Override
	@Transactional
	public void updateByPrimaryKey(Vendor vendor,String[] cName,String[] cValue,String[] eId){
		vendorMapper.updateByPrimaryKeyWithBLOBs(vendor);
		if(Common.isNullOrEmpty(cName) || Common.isNullOrEmpty(cValue)  ||
				cName.length == 0 || cValue.length == 0 ){
			return;
		}
		for(int i=0;i<cName.length;i++){
			if(!Common.isEmptyString(cName[i]) || !Common.isEmptyString(cValue[i])){
				continue;
			}
			ExtraColumn e = new  ExtraColumn();
			e.setPid(vendor.getId());
			e.settType(Short.parseShort("0"));//0表示厂商，1表示产品
			e.setcName(cName[i]);
			e.setcValue(cValue[i]);
			if(Integer.parseInt(eId[i]) == 0){
				extraColumnMapper.insertSelective(e);
			}else{
				e.setId(Integer.parseInt(eId[i]));
				extraColumnMapper.updateByPrimaryKeySelective(e);				
			}
		}
		
	}

	@Override
	@Transactional
	public void deleteByPrimaryKey(Integer id) {
		int  count = productMapper.findProductCount(id);
		if(count >0){
			return;
		}
		ExtraColumn e = new ExtraColumn();
		e.setPid(id);
		e.settType(Short.parseShort("0"));//0表示厂商，1表示产品
		extraColumnMapper.deleteByPIdAndType(e);
		Image img = new Image();
		img.setPid(id);
		img.setType(Short.parseShort("0"));//0表示厂商，1表示产品
		imageMapper.delImagesByPidAndType(img);
		vendorMapper.deleteByPrimaryKey(id);
	}

	public VendorMapper getVendorMapper() {
		return vendorMapper;
	}

	public void setVendorMapper(VendorMapper vendorMapper) {
		this.vendorMapper = vendorMapper;
	}

	@Override
	public List<DropDownDto> selectVendorNameList() {
		List<DropDownDto> vendorNameList = vendorMapper.selectVendorNameList();
		return vendorNameList;
	}

	@Override
	public int findVendorNameCount(Map<Object,Object> map) {
		return vendorMapper.findVendorNameCount(map);
	}

	@Override
	public List<Vendor> findAllVendors() {
		return vendorMapper.findAllVendors();
	}

	@Override
	public VendorView selectVendorViewById(Integer vendorId) {
		if(Common.isNullOrEmpty(vendorId)){
			logger.error("传入参数为空");
			return null;
		}
		VendorView vv = new VendorView();
		Vendor vendor = vendorMapper.selectByPrimaryKey(vendorId);
		Short tType = 0;
		ExtraColumn ec = new ExtraColumn();
		ec.settType(tType);//0表示厂商，1表示产品
		ec.setPid(vendorId);
		List<ExtraColumn> ecList = extraColumnMapper.selectByPIdAndType(ec);
		vv.setVendor(vendor);
		vv.setExtraColumnList(ecList);
		return vv;
	}

	@Override
	public int delExtraColumnById(String id) {
		
		int count = 0;
		if(!Common.isEmptyString(id)){
			logger.error("传入参数为空");
			return count;
		}
		count = extraColumnMapper.deleteByPrimaryKey(Integer.parseInt(id));
		return count;
	}

	@Override
	public Vendor getMaxIdVendor() {
		return vendorMapper.getMaxIdVendor();
	}

	@Override
	public int updateVendor(Vendor vendor) {
		return vendorMapper.updateByPrimaryKeySelective(vendor);
	}

	@Override
	public Vendor findByVendorName(String vendorName) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("vendorName", vendorName);
		map.put("id", null);
		int count = vendorMapper.findVendorNameCount(map);
		if(count == 1){
			return vendorMapper.selectVendorByName(vendorName);
		}
		return null;
	}





}
